package com.itboye.pondteam.app;

import android.app.Activity;
import android.content.Context;
import android.support.multidex.MultiDexApplication;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.itboye.pondteam.bean.DeviceListBean;
import com.itboye.pondteam.presenter.UserPresenter;
import com.itboye.pondteam.utils.Const;
import com.itboye.pondteam.utils.LogUtils;
import com.itboye.pondteam.utils.languageutil.LocaleManager;
import com.itboye.pondteam.volley.ResultEntity;

import java.lang.ref.WeakReference;
import java.util.*;

import static com.itboye.pondteam.utils.EmptyUtil.getSp;
import static com.itboye.pondteam.utils.MyTimeUtil.getTimeZone;
import static com.itboye.pondteam.utils.ScreenUtil.getIMEI;

//import com.itboye.pondteam.utils.languageutil.LanguageUtil;

/**
 * Created by admin on 2016/12/6.
 */

public class MyApplication extends MultiDexApplication implements Observer {

    private static RequestQueue queue;
    public List<WeakReference<Activity>> activityList = new LinkedList<>();
    public static MyApplication instance;
    public DeviceListBean mEditDeviceInfo;
    UserPresenter userPresenter = new UserPresenter(this);
    public boolean isDebug;
    public HashMap<String, String> roomAuth = new HashMap<>();

    public List<WeakReference<Activity>> getActivityList() {
        return activityList;
    }

    public void setActivityList(List<WeakReference<Activity>> activityList) {
        this.activityList = activityList;
    }


    public static MyApplication getInstance() {
        return instance;
    }

    public static void setInstance(MyApplication instance) {
        MyApplication.instance = instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
//        isDebug = false;

        queue = Volley.newRequestQueue(this);
        instance = this;
        if (getPackageName().contains("pondteam") || getPackageName().contains("pondlink")) {
            userPresenter.setBaseUrl(Const.pondlink_wrapUrl);
//            initLanguage();
        }
        updateMobile(this);
    }

    public void initLanguage() {
//        LocaleManager.setNewLocale()
//        Configuration config = getBaseContext().getResources()
//                .getConfiguration();
//        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
//        if (getPackageName().toLowerCase().contains("pondlink".toLowerCase()) || getPackageName().toLowerCase().contains("pondteam".toLowerCase())) {
//            String lan = getLanguageForPondTeam();
//            if (lan.isEmpty()) {
//                config.locale = new Locale("en", "GB");
//            } else {
//                config.locale = new Locale(lan.split("_")[0], lan.split("_")[1]);
//            }
//        } else {
//            String lan = getLanguage();
//            LogUtils.v("Application_tag", "语言：" + lan);
//            config.locale = new Locale(lan.split("_")[0], lan.split("_")[1]);
//        }
//        getResources().updateConfiguration(config, displayMetrics);
//        updateMobile();
    }


    public void updateMobile(Context conext) {
        int timezone = getTimeZone();
        String lang = LocaleManager.getLanguage(conext);
        String device_id = getIMEI(conext);
        userPresenter.updateMobileMsg(getSp(conext, Const.UID), device_id, updateLL(lang), timezone + "");
    }

    public String updateLL(String lang) {
        lang = lang.replace("_", "-");
        if (lang.startsWith("zh")) {
        } else if (lang.startsWith("en")) {
            lang = "en";
        }
        LogUtils.v("服务器语言===" + lang.toLowerCase());
        return lang.toLowerCase();
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(LocaleManager.setLocale(base));
//        updateMobile(base);
//        Log.d(TAG, "attachBaseContext");
    }

    public void addActivity(Activity activity) {
        activityList.add(new WeakReference<>(activity));
    }

    // 遍历所有Activity并finish
    public void exit() {
        for (WeakReference<Activity> activity : activityList) {
            if (activity.get() != null) {
                activity.get().finish();
            }
        }
//        System.exit(0);
    }

    /**
     * @return the queue
     */
    public static RequestQueue getQueue() {
        return queue;
    }

    //
    public static <T> void addRequest(Request<T> request) {
        request.addMarker("itboye");
        getQueue().add(request);
    }

    @Override
    public void update(Observable o, Object data) {
        ResultEntity result = (ResultEntity) data;
        if (result != null) {
            if (result.getCode() != 0) {
//                MAlert.alert(result.getMsg()+"");
//                throw new RuntimeException("报错啦");
//                return;
            }
            if (result.getEventType() == UserPresenter.updateMobileMsg_success) {
//                MAlert.alert(result.getData());
            } else if (result.getEventType() == UserPresenter.updateMobileMsg_fail) {
//                MAlert.alert(result.getData());
            }
        }
    }
}
